package com.stox.charting.ui.swing.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import com.stox.charting.ui.swing.widget.ChartMenuItem;
import com.stox.core.model.intf.HasBars;
import com.stox.core.ui.swing.widget.MainFrame;
import com.stox.core.ui.swing.wrapper.JTree;
import com.stox.core.util.PropertiesUtil;
import com.stox.ioc.Container;
import com.stox.resources.i18n.MessageSource;

public abstract class AbstractChartAction implements ActionListener {

	private int maxChartsLimit;
	
	public AbstractChartAction() {
		PropertiesUtil propertiesUtil = Container.get(PropertiesUtil.class);
		maxChartsLimit = Integer.parseInt(propertiesUtil.get("max.charts"));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		List<HasBars> hasBars = null;
		
		if(e.getSource() instanceof ChartMenuItem){
			ChartMenuItem chartMenuItem = (ChartMenuItem)e.getSource();
			if(chartMenuItem.getParent() instanceof JPopupMenu){
				JPopupMenu popupMenu = (JPopupMenu)chartMenuItem.getParent();
				if(popupMenu.getInvoker() instanceof JTree){
					JTree tree = (JTree)popupMenu.getInvoker();
					hasBars = retriveHasBars(tree);
				}
			}
		}else if(e.getSource() instanceof JTree){
			JTree tree = (JTree)e.getSource();
			hasBars = retriveHasBars(tree);
		}
		
		if(!validateMaximumChartLimit(hasBars, maxChartsLimit)) return;
		if(!validateMinimumChartLimit(hasBars, 1)) return;
		actionPerformed(e, hasBars);
	}

	public boolean validateMaximumChartLimit(List<HasBars> hasBars, int maxChartsLimit){
		if(hasBars.size() > maxChartsLimit){
			JOptionPane.showMessageDialog(
					MainFrame.getInstance(), 
					MessageSource.getMessage("Maximum.charts.limit", maxChartsLimit, hasBars.size()), 
					MessageSource.getMessage("Too.many.charts"), 
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}
	
	public boolean validateMinimumChartLimit(List<HasBars> hasBars, int minChartsLimit){
		if(hasBars == null || hasBars.isEmpty()){
			JOptionPane.showMessageDialog(
					MainFrame.getInstance(), 
					MessageSource.getMessage("Nothing.selected"), 
					MessageSource.getMessage("Error"), 
					JOptionPane.ERROR_MESSAGE);
			return false;
		}else if(hasBars != null && hasBars.size() < minChartsLimit){
			JOptionPane.showMessageDialog(
					MainFrame.getInstance(), 
					MessageSource.getMessage("Minimum.Charts.Limit", minChartsLimit), 
					MessageSource.getMessage("Error"), 
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}
	
	public abstract void actionPerformed(ActionEvent e, List<HasBars> hasBars);
	
	private List<HasBars> retriveHasBars(JTree tree){
		List<HasBars> list = new ArrayList<HasBars>();
		TreePath[] paths = tree.getSelectionPaths();
		if(paths != null && paths.length > 0){
			for(TreePath path : paths){
				DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
				if(node.getUserObject() instanceof HasBars){
					list.add((HasBars)node.getUserObject());
				}
			}
		}
		return list;
	}
	
}
